﻿using System;
using System.Collections.Generic;

namespace UtilZ.Dotnet.Ex.Json
{
    /// <summary>
    /// json转换器管理类
    /// </summary>
    public class JsonConverterManger
    {
        private readonly static Dictionary<Type, IJsonValueConverter> _defaultJsonValueConverterDic = null;

        static JsonConverterManger()
        {
            _defaultJsonValueConverterDic = new Dictionary<Type, IJsonValueConverter>()
            {
                { typeof(char),CharJsonValueConverter.Default},
                { typeof(string),StringJsonValueConverter.Default},
                { typeof(bool),BoolJsonValueConverter.Default},

                { typeof(sbyte),BaseValueTypeJsonValueConverter.Default},
                { typeof(byte),BaseValueTypeJsonValueConverter.Default},
                { typeof(Int16),BaseValueTypeJsonValueConverter.Default},
                { typeof(UInt16),BaseValueTypeJsonValueConverter.Default},
                { typeof(Int32),BaseValueTypeJsonValueConverter.Default},
                { typeof(UInt32),BaseValueTypeJsonValueConverter.Default},
                { typeof(Int64),BaseValueTypeJsonValueConverter.Default},
                { typeof(UInt64),BaseValueTypeJsonValueConverter.Default},

                { typeof(float),BaseValueTypeJsonValueConverter.Default},
                { typeof(double),BaseValueTypeJsonValueConverter.Default},
                { typeof(decimal),BaseValueTypeJsonValueConverter.Default},

                { typeof(DateTime),DateTimeJsonValueConverter.Default},
                { typeof(DateTimeOffset),DateTimeOffsetJsonValueConverter.Default},
                #if CORE
                { typeof(DateOnlyJsonValueConverter),DateOnlyJsonValueConverter.Default},
                { typeof(TimeOnly),TimeOnlyJsonValueConverter.Default},
                #else
                #endif

                { typeof(object),ObjectJsonValueConverter.Default},
                { typeof(Enum),EnumJsonValueConverter.Default},
                { typeof(IntPtr),IntPtrJsonValueConverter.Default},
            };
        }



        /// <summary>
        /// 根据值类型获取默认转换器
        /// </summary>
        /// <param name="valueType">值类型</param>
        /// <returns>默认转换器</returns>
        /// <exception cref="NotImplementedException"></exception>
        public static IJsonValueConverter GetDefaultConverter(Type valueType)
        {
            if (valueType.IsEnum)
            {
                //枚举需要将具体的枚举类型重新映射成Enum类型
                valueType = typeof(Enum);
            }

            IJsonValueConverter defaultConverter;
            if (_defaultJsonValueConverterDic.TryGetValue(valueType, out defaultConverter))
            {
                return defaultConverter;
            }
            else
            {
                return ObjectJsonValueConverter.Default;
            }

            //throw new NotImplementedException($"未实现的类型{valueType.FullName}");
        }


        /// <summary>
        /// 根据值类型获取默认转换器[获取到默认转换器返回true,没获取到返回false]
        /// </summary>
        /// <param name="valueType">值类型</param>
        /// <param name="defaultConverter">默认转换器</param>
        /// <returns>获取到默认转换器返回true,没获取到返回false</returns>
        public static bool TryGetDefaultConverter(Type valueType, out IJsonValueConverter defaultConverter)
        {
            return _defaultJsonValueConverterDic.TryGetValue(valueType, out defaultConverter);
        }


    }
}
